package nsu.grandfinal;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class UserSetting extends Activity {
	private DatabaseHandle	dbHandle;
	private EditText				teName;
	private TextView				tvAddress;
	private double					laitude;
	private double					longitude;
	private int							ID;
	private int							mode;
	private int							range;
	private int							hour;
	private int							minute;
	private int							day;
	private int							alaramcode;
	private String					address;
	private String					featureName;
	private long						info;
	private RadioGroup			mode_group;
	private RadioGroup			range_group;
	private RadioGroup			day_group;
	private Calendar				calendar;
	private TimePicker			timePicker;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.usersetting);
		calendar = Calendar.getInstance();
		range_group = (RadioGroup) findViewById(R.id.range_group);
		mode_group = (RadioGroup) findViewById(R.id.mode_group);
		day_group = (RadioGroup) findViewById(R.id.day_group);
		teName = (EditText) findViewById(R.id.teName);
		tvAddress = (TextView) findViewById(R.id.tvaddress2);
		Button btSave = (Button) findViewById(R.id.btnSave);
		Button btReset = (Button) findViewById(R.id.btnReset);
		timePicker = (TimePicker) findViewById(R.id.tImePicker);
		
		timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
				calendar.set(Calendar.MINUTE, minute);
			}
		});
		btSave.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (alaramcode == 0) alaramcode = (int) (Math.random() * 1000000);
				address = tvAddress.getText().toString();
				featureName = teName.getText().toString();
				range = getRadioButton("range");
				mode = getRadioButton("mode");
				hour = calendar.get(Calendar.HOUR_OF_DAY);
				minute = calendar.get(Calendar.MINUTE);
				day = getRadioButton("day");
				dbHandle = new DatabaseHandle(UserSetting.this);
				dbHandle.open();
				if (dbHandle.consCursor.getCount() == 0) dbHandle.createNote(address, featureName, range, laitude, longitude, mode, hour, minute, day, alaramcode);
				else if (ID == info) dbHandle.updateNote(ID, address, featureName, range, laitude, longitude, mode, hour, minute, day, alaramcode);
				else dbHandle.createNote(address, featureName, range, laitude, longitude, mode, hour, minute, day, alaramcode);
				dbHandle.close();
				Intent sendIntent = new Intent(UserSetting.this, AlarmReceive.class);
				sendIntent.putExtra("id", ID);
				sendIntent.putExtra("featureName", featureName);
				sendIntent.putExtra("address", address);
				sendIntent.putExtra("Lat", laitude);
				sendIntent.putExtra("Lon", longitude);
				sendIntent.putExtra("mode", mode);
				sendIntent.putExtra("range", range);
				sendIntent.putExtra("day", day);
				sendIntent.putExtra("alarmcode", alaramcode);
				sendIntent.putExtra("sender", "UserSetting");
				PendingIntent pender = PendingIntent.getBroadcast(UserSetting.this, alaramcode, sendIntent, PendingIntent.FLAG_UPDATE_CURRENT);
				AlarmManager alarm = (AlarmManager) UserSetting.this.getSystemService(Context.ALARM_SERVICE);
				alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pender);
				finish();
			}
		});
		btReset.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				onResume();
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		day = 0;
		int LD;
		dbHandle = new DatabaseHandle(this).open();
		Intent getIntent = getIntent();
		range = 50;
		mode = 1;
		if (getIntent.getStringExtra("sender") != null) {
			if (dbHandle.consCursor.getCount() > 0) LD = dbHandle.getLastId();
			else LD = 0;
			ID = getIntent.getIntExtra("id", LD + 1);
			if (getIntent.getStringExtra("sender").equals("saveList")) {
				info = getIntent.getLongExtra("infoID", 0);
				range = getIntent.getIntExtra("range", 50);
				alaramcode = dbHandle.getIntData(ID, "alarmcode");
				hour = dbHandle.getIntData(ID, "hour");
				minute = dbHandle.getIntData(ID, "minute");
				day = dbHandle.getIntData(ID, "day");
				mode = dbHandle.getIntData(ID, "mode");
				
				calendar.set(Calendar.HOUR_OF_DAY, hour);
				calendar.set(Calendar.MINUTE, minute);
				calendar.set(Calendar.DAY_OF_WEEK, day);
			}
			featureName = getIntent.getStringExtra("featureName");
			address = getIntent.getStringExtra("address");
			laitude = getIntent.getDoubleExtra("Lat", 0);
			longitude = getIntent.getDoubleExtra("Lon", 0);
			Log.i("Lat:", laitude + "");
			Log.i("Lon:", longitude + "");
			teName.setText(featureName);
			tvAddress.setText(address);
			setRadioButton(range, "range");
			setRadioButton(mode, "mode");
			if (day == 0) {
				day = calendar.get(Calendar.DAY_OF_WEEK);
			}
			setRadioButton(day, "day");
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		dbHandle.close();
	}
	
	private void setRadioButton(int data, String group) {
		int rid = getResources().getIdentifier("nsu.grandfinal:id/radio_" + group + "_" + data, null, null);
		if (group.equals("mode")) mode_group.check(rid);
		else if (group.equals("range")) range_group.check(rid);
		else day_group.check(rid);
	}
	
	private int getRadioButton(String s) {
		RadioGroup rg = null;
		if (s.equals("mode")) rg = mode_group;
		else if (s.equals("range")) rg = range_group;
		else rg = day_group;
		RadioButton rb2 = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
		return Integer.parseInt((String) rb2.getContentDescription());
	}
}